package com.example.demo.calculator;

import com.example.demo.Redis.RedisUtil;
import com.example.demo.common.RedisConstants;
import com.example.demo.pojo.Identity;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

@Service
public class CalculatorEngine {

    @Resource
    private RedisUtil redisUtil;

    public int trustCalculate(Identity identity){
        int user = SwitchToInt(RedisConstants.user_prefix + identity.getIdentity_user().getUser());
        int dev = SwitchToInt(RedisConstants.mac_prefix+identity.getIdentity_device().getDevice_Mac());
        int ip = SwitchToInt(RedisConstants.ip_prefix+identity.getIdentity_env().getIp());
        int version = SwitchToInt(RedisConstants.version_prefix+identity.getIdentity_env().getVersion());
        int os = SwitchToInt(RedisConstants.os_prefix+identity.getIdentity_env().getOS());

        int trust = user+dev+ip+version+os;
        return trust;
    }

    private int SwitchToInt(String redisKey){
        Object obj = redisUtil.get(redisKey);
        if(null == obj){
            return 0;
        }
        return Integer.parseInt(obj.toString());
    }

}
